// Unless explicitly stated otherwise all files in this repository are licensed
// under the MIT License.
// This product includes software developed at Guance Cloud (https://www.guance.com/).
// Copyright 2021-present Guance, Inc.

// Package tracer defined basic trace wraps.
package tracer

import (
	"net"

	"github.com/GuanceCloud/cliutils/logger"
	"gopkg.in/CodapeWild/dd-trace-go.v1/ddtrace/tracer"
)

var log = logger.DefaultSLogger("ddtrace")

type DiscardLogger struct{}

func (*DiscardLogger) Log(msg string) {}

type StdLogger struct{}

func (*StdLogger) Log(msg string) {
	log.Debug(msg)
}

type Tracer struct {
	Host    string `toml:"host" yaml:"host"`       // env: DD_AGENT_HOST
	Port    string `toml:"port" yaml:"port"`       // env: DD_TRACE_AGENT_PORT
	Service string `toml:"service" yaml:"service"` // env: DD_SERVICE
	Version string `toml:"version" yaml:"version"` // env: DD_VERSION
	Env     string `toml:"env" yaml:"env"`         // env: DD_ENV

	agentAddr string

	LogsStartup  bool                   `toml:"logs_startup" yaml:"logs_startup"`   // env: DD_TRACE_STARTUP_LOGS
	Debug        bool                   `toml:"debug" yaml:"debug"`                 // env: DD_TRACE_DEBUG
	TraceEnabled bool                   `toml:"trace_enabled" yaml:"trace_enabled"` // env: DD_TRACE_ENABLED
	Tags         map[string]interface{} `toml:"tags" yaml:"tags"`                   // env: DD_TAGS
}

func (t *Tracer) GetStartOptions(opts ...StartOption) []tracer.StartOption {
	if t.Tags == nil {
		t.Tags = make(map[string]interface{})
	}

	for i := range opts {
		opts[i](t)
	}

	if t.Host == "" {
		t.Host = "127.0.0.1"
	}
	if t.Port == "" {
		t.Port = "9529"
	}
	t.agentAddr = net.JoinHostPort(t.Host, t.Port)

	startOpts := []tracer.StartOption{
		tracer.WithAgentAddr(t.agentAddr),
		tracer.WithService(t.Service),
		tracer.WithServiceVersion(t.Version),
		tracer.WithLogStartup(t.LogsStartup),
		tracer.WithDebugMode(t.Debug),
		tracer.WithEnv(t.Env),
	}
	for k, v := range t.Tags {
		startOpts = append(startOpts, tracer.WithGlobalTag(k, v))
	}

	return startOpts
}
